﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using NHibernateMappingSample.DataAccess;
using NHibernateMappingSample.Entities;
using ProjectBase.Data.NHibernate;

namespace NHibernateMappingSample.Driver
{
   class Program
   {
      static void Main(string[] args)
      {
         NHibernateSession.Init(new SimpleSessionStorage(), null);
         //GetAllAddresses();
         //GetCustomer();
         //GetOrdersForCustomer();
         AddCustomerOrder();
         Console.ReadLine();
      }
      private static void GetAllAddresses()
      {
         IList<Address> addresses = new GenericDao<Address>().LoadAll();
         PrintAddress(addresses);
      }

      private static void PrintAddress(IList<Address> addresses)
      {
         foreach (Address address in addresses)
         {
            Console.WriteLine("AddressID: {0}, Street :{1}, City: {2}, State: {3}, Postal Code: {4}",
                              address.AddressID,
                              address.AddressLine1 + address.AddressLine2,
                              address.City, address.StateProvince, address.PostalCode);
         }
      }

      private static void GetCustomer()
      {
         Customer customer = new GenericDao<Customer>().Load((2));
            Console.WriteLine("Customer ID :{0}, First Name {1}, Last Name {2}", customer.CustomerID, customer.FirstName, customer.LastName);
            PrintAddress(customer.Addresses);

      }

      private static void GetOrdersForCustomer()
      {

         IList<SalesOrderHeader> orders = new SalesOrdersDao().GetOrdersForCustomer(16);
            foreach (var order in orders)
            {
               Console.WriteLine("OrderID : {0}, OrderDate : {1}, CustomerID {2}", order.SalesOrderID, order.OrderDate, order.BelongsToCustomer.CustomerID);
            }
        

      }

  


      private static void AddCustomerOrder()
      {
         using (NHibernateSession.Current)
         {
            var customerDao = new GenericDao<Customer>();
            Customer customer = customerDao.Load((2));
            Product product = new GenericDao<Product>().Load(712);
            SalesOrderHeader order = new SalesOrderHeader();
            order.DueDate = DateTime.Today;
            order.OrderDate = DateTime.Today;
            order.ShipDate = DateTime.Today + new TimeSpan(7, 0, 0, 0);
            order.ModifiedDate = DateTime.Now;
            order.BelongsToCustomer = customer;
            order.ShipMethod = "FEDEX";
            order.OrderDetails.Add(new SalesOrderDetail {ParentOrder = order, UnitPrice = 10, UnitPriceDiscount = 0.2m,ModifiedDate = DateTime.Now,
            Product = product,OrderQty = 5});
            new SalesOrdersDao().Save(order);
         }
      }
   }
}
